/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.classes;

/**
 * This class represents a grade data type. It takes the mark, total, and weight
 * as required data. It contains some helpful utility methods to calculate the
 * percentages as well.
 * 
 * @author 
 * 
 */
public class GenericGrade {

	private double _marks;
	private double _total;

	private double _percentage;
	private double _percentageDecimal;

	private double _weight; // the percentage weight to a course

	public double get_marks() {
		return _marks;
	}

	public void set_marks(double _marks) {
		this._marks = _marks;
	}

	public double get_total() {
		return _total;
	}

	public void set_total(double _total) {
		this._total = _total;
	}

	public double get_percentage() {
		return _percentage;
	}

	public double calculatePercentage() {
		this._percentage = Math.floor(calculatePercentageDecimal() * 100+0.5);
		return this._percentage;
	}

	public double calculatePercentageDecimal() {
		this._percentageDecimal = (_marks / _total);
		return this._percentageDecimal;
	}

	/**
	 * This will return the total percentage you will get based on the weight.
	 * 
	 * @return the weight in percentage
	 */
	public double calculatePercentageWeight() {
		return this._percentageDecimal * this._weight;
	}

	/**
	 * This constructor is used for setting up final grade. <br/>
	 * e.g., the final grade of a course
	 * 
	 * @param _marks
	 *            the final marks
	 */
	public GenericGrade(double _marks) {
		this(_marks, 100, 100);
	}

	/**
	 * This is a general constructor that can be used for any purpose.
	 * 
	 * @param _marks
	 *            the marks
	 * @param _total
	 *            the total
	 * @param _weight
	 *            the weight of a course (should be in percentage, e.g, 45%)
	 */
	public GenericGrade(double _marks, double _total, double _weight)
			throws IllegalArgumentException {
		if (_marks < 0) {
			throw new IllegalArgumentException(
					"Argument \"_marks\" cannot be less than 0");
		}

		if (_total < 0) {
			throw new IllegalArgumentException(
					"Argument \"_total\" cannot be less than  0");
		}

		if (_total < _marks) {
			throw new IllegalArgumentException(
					"Argument \"_total\" cannot be less than \"_marks\"");
		}

		if (_weight < 0) {
			throw new IllegalArgumentException(
					"Argument \"_weight\" cannot be less than  0");
		}

		this._marks = _marks;
		this._total = _total;
		this._weight = _weight;
		// calculate the percentage right away
		calculatePercentage();
		calculatePercentageWeight();
	}

	public double get_weight() {
		return _weight;
	}

	public void set_weight(double _weight) {
		this._weight = _weight;
	}

}
